package pl.vm.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import pl.vm.domain.OfficialTeam;

@Repository
@Transactional
public class OfficialTeamDao {

	@Autowired
	private SessionFactory sessionFactory;

	public final OfficialTeam get(long id) {
		return (OfficialTeam) sessionFactory.getCurrentSession().get(OfficialTeam.class, id);
	}

	public final void refresh(OfficialTeam entity) {
		sessionFactory.getCurrentSession().refresh(entity);
	}

	public void save(OfficialTeam entity) {
		sessionFactory.getCurrentSession().save(entity);
	}

	public void saveOrUpdate(OfficialTeam entity) {
		sessionFactory.getCurrentSession().saveOrUpdate(entity);
	}

	public void update(OfficialTeam entity) {
		sessionFactory.getCurrentSession().update(entity);
	}

	public final void delete(OfficialTeam entity) {
		sessionFactory.getCurrentSession().delete(entity);
	}

	public void evict(OfficialTeam entity) {
		sessionFactory.getCurrentSession().evict(entity);
	}

	public void persist(OfficialTeam entity) {
		sessionFactory.getCurrentSession().persist(entity);
	}

	public final void flush() {
		sessionFactory.getCurrentSession().flush();
	}

	public final void clearSession() {
		sessionFactory.getCurrentSession().clear();
	}

	@SuppressWarnings("unchecked")
	public final List<OfficialTeam> loadAll() {
		return sessionFactory.getCurrentSession().createQuery("from OfficialTeam").list();
	}

	public final boolean exists(int id) {
		Criteria criteria = sessionFactory.getCurrentSession()
				.createCriteria(OfficialTeam.class).add(Restrictions.idEq(id));
		criteria.setProjection(Projections.rowCount());
		return (Integer) criteria.uniqueResult() > 0;
	}
}
